################################################################################
## Group Identities and Parliamentary Debates: Replication package
## Fiva, Nedregård and Øien (2025)

# Description:

## Code to make Table A4: "Correlation matrix for politicians' background characteristics"

################################################################################

# Packages

library(data.table)
library(dplyr)
library(xtable)

# Directories (wd is set by master.R)
data.dir           <- "../data/2_processed_data"
tab.dir            <- "../results/tables"
in_text            <- "../results/in_text"

DT <- fread(file = paste(data.dir, "speeches_session_lemma.csv", sep = "/"), encoding="UTF-8")


DT$bloc <- ifelse(DT$bloc=="Sp" & as.numeric(substr(DT$session,1,4))>=2005, "A", DT$party)
DT$bloc <- ifelse(DT$bloc=="Sp" & as.numeric(substr(DT$session,1,4))<2005, "H", DT$party)
DT$bloc <- ifelse(DT$bloc %in% c( "FrP", "H", "KrF", "V"), "H", "V")

DT <- DT %>%  select("bloc", "female", "age_cat", "town", "occupation") %>%
  mutate(bloc=ifelse(bloc=="H", 1, 0),
       #  female=ifelse(female=="kvinne", 1, 0),
         age_cat=ifelse(age_cat=="old", 1, 0),
         town=ifelse(town=="urban", 1, 0),
         occupation=ifelse(occupation=="white", 1, 0))


cor_tab <- round(cor(DT, use="complete.obs"),3)
cor_tab[upper.tri(cor_tab)]<-""
cor_tab<-as.data.frame(cor_tab)

rownames(cor_tab) <- c("Right-wing", "Female", "Old", "Urban", "White-collar")
#colnames(cor_tab) <- c("Right", "Female", "Old", "Town", "White")


cor_tab <- xtable(cor_tab)
print(cor_tab, file=paste(tab.dir, "tabA4.txt", sep = "/"), hline.after = NULL, 
      only.contents=T, include.colnames=F, comment = F)






